package com.ruoyi.ocean.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ocean.domain.bo.OceanLogTotalBo;
import com.ruoyi.ocean.domain.vo.OceanLogTotalVo;
import com.ruoyi.ocean.domain.OceanLogTotal;
import com.ruoyi.ocean.mapper.OceanLogTotalMapper;
import com.ruoyi.ocean.service.IOceanLogTotalService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 操作日志统计Service业务层处理
 *
 * @author linziheng
 * @date 2024-05-08
 */
@RequiredArgsConstructor
@Service
public class OceanLogTotalServiceImpl implements IOceanLogTotalService {

    private final OceanLogTotalMapper baseMapper;
    @Override
    public List<String> getMonthInit(){
        return baseMapper.getMonthInit();
    }
    /**
     * 查询操作日志统计
     */
    @Override
    public OceanLogTotalVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询操作日志统计列表
     */
    @Override
    public TableDataInfo<OceanLogTotalVo> queryPageList(OceanLogTotalBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<OceanLogTotal> lqw = buildQueryWrapper(bo);
//        Page<OceanLogTotalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<OceanLogTotalVo> result = baseMapper.selectTotalPage(pageQuery.build(), bo);
        List<OceanLogTotalVo> list = baseMapper.selectTotal(bo);
        List<OceanLogTotalVo> org = result.getRecords();
        org.forEach(item -> {
            for (OceanLogTotalVo item1 : list) {
                if(item.getAccountId().equals(item1.getAccountId()) && item.getMonth().equals(item1.getMonth())){
                    item.setDTotal(item1.getDTotal());
                    item.setQTotal(item.getTotal() - item1.getDTotal());
                }
            }
        });
        org.forEach(item -> {
           if(item.getDTotal()==null){
               item.setDTotal(0L);
               item.setQTotal(item.getTotal() - item.getDTotal());
           }
        });
        result.setRecords(org);
        return TableDataInfo.build(result);
    }


    @Override
    public TableDataInfo<OceanLogTotalVo> queryPageListQC(OceanLogTotalBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<OceanLogTotal> lqw = buildQueryWrapper(bo);
//        Page<OceanLogTotalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<OceanLogTotalVo> result = baseMapper.selectTotalPageQC(pageQuery.build(), bo);
        List<OceanLogTotalVo> list = baseMapper.selectTotalQC();
        List<OceanLogTotalVo> org = result.getRecords();
        org.forEach(item -> {
            for (OceanLogTotalVo item1 : list) {
                if(item.getAccountId().equals(item1.getAccountId()) && item.getCreateTime().getTime()==item1.getCreateTime().getTime()){
                    item.setDTotal(item1.getDTotal());
                    item.setQTotal(item.getTotal() - item1.getDTotal());
                }
            }
        });
        org.forEach(item -> {
            if(item.getDTotal()==null){
                item.setDTotal(0L);
                item.setQTotal(item.getTotal() - item.getDTotal());
            }
        });
        result.setRecords(org);
        return TableDataInfo.build(result);
    }

    @Override
    public List<OceanLogTotalVo> queryNoPageListAD(OceanLogTotalBo bo) {
//        LambdaQueryWrapper<OceanLogTotal> lqw = buildQueryWrapper(bo);
//        Page<OceanLogTotalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<OceanLogTotalVo> org = baseMapper.selectTotalPage(bo);
        List<OceanLogTotalVo> list = baseMapper.selectTotal(bo);
        org.forEach(item -> {
            for (OceanLogTotalVo item1 : list) {
                if(item.getAccountId().equals(item1.getAccountId()) && item.getMonth().equals(item1.getMonth())){
                    item.setDTotal(item1.getDTotal());
                    item.setQTotal(item.getTotal() - item1.getDTotal());
                }
            }
        });
        org.forEach(item -> {
            if(item.getDTotal()==null){
                item.setDTotal(0L);
                item.setQTotal(item.getTotal() - item.getDTotal());
            }
        });

        return org;
    }

    @Override
    public List<OceanLogTotalVo> queryNoPageListQC(OceanLogTotalBo bo) {
//        LambdaQueryWrapper<OceanLogTotal> lqw = buildQueryWrapper(bo);
//        Page<OceanLogTotalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<OceanLogTotalVo> org = baseMapper.selectTotalPageQC(bo);
        List<OceanLogTotalVo> list = baseMapper.selectTotalQC();
        org.forEach(item -> {
            for (OceanLogTotalVo item1 : list) {
                if(item.getAccountId().equals(item1.getAccountId()) && item.getCreateTime().getTime()==item1.getCreateTime().getTime()){
                    item.setDTotal(item1.getDTotal());
                    item.setQTotal(item.getTotal() - item1.getDTotal());
                }
            }
        });
        org.forEach(item -> {
            if(item.getDTotal()==null){
                item.setDTotal(0L);
                item.setQTotal(item.getTotal() - item.getDTotal());
            }
        });

        return org;
    }
    /**
     * 查询操作日志统计列表
     */
    @Override
    public List<OceanLogTotalVo> queryList(OceanLogTotalBo bo) {
        LambdaQueryWrapper<OceanLogTotal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceanLogTotal> buildQueryWrapper(OceanLogTotalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OceanLogTotal> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), OceanLogTotal::getAccountId, bo.getAccountId());
        lqw.eq(bo.getTotal() != null, OceanLogTotal::getTotal, bo.getTotal());
        lqw.eq(bo.getDTotal() != null, OceanLogTotal::getDTotal, bo.getDTotal());
        lqw.eq(bo.getQTotal() != null, OceanLogTotal::getQTotal, bo.getQTotal());
        lqw.eq(StringUtils.isNotBlank(bo.getIsOk()), OceanLogTotal::getIsOk, bo.getIsOk());
        return lqw;
    }

    /**
     * 新增操作日志统计
     */
    @Override
    public Boolean insertByBo(OceanLogTotalBo bo) {
        OceanLogTotal add = BeanUtil.toBean(bo, OceanLogTotal.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改操作日志统计
     */
    @Override
    public Boolean updateByBo(OceanLogTotalBo bo) {
        OceanLogTotal update = BeanUtil.toBean(bo, OceanLogTotal.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceanLogTotal entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作日志统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
